package com.hualan.sfzy.controller.course;

import com.hualan.sfzy.entity.course.CourseClassify;
import com.hualan.sfzy.service.course.CourseClassifyService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class CourseClassifyController {
    @Autowired
    private CourseClassifyService courseClassifyService;
    @PutMapping("/courseclassify")
    public Result updateCourseClassify(@RequestBody CourseClassify courseClassify) {
        return courseClassifyService.edit(courseClassify)>0?Result.success():Result.error();
    }
    @PostMapping("/courseclassify")
    public Result add(@RequestBody CourseClassify courseClassify) {
        return courseClassifyService.add(courseClassify)>0?Result.success():Result.error();
    }
    @DeleteMapping("/courseclassify/{id}")
    public Result remove(@PathVariable int id) {
        // 先检查是否有关联课程
        if (courseClassifyService.hasCourses(id)) {
            return Result.error("该分类下存在课程，不允许删除");
        }
        return courseClassifyService.remove(id) > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping("/courseclassify")
    public Result removeBatch(Integer[] ids) {
        // 检查批量删除的每个分类
        for (Integer id : ids) {
            if (courseClassifyService.hasCourses(id)) {
                return Result.error("部分分类下存在课程，不允许删除");
            }
        }
        return courseClassifyService.removeBatch(ids) > 0 ? Result.success() : Result.error();
    }
    @GetMapping("/courseclassify")
    public Result find() {
        List<CourseClassify> list = courseClassifyService.find();
        return Result.success(list);
    }
    @GetMapping("/courseclassify/{id}")
    public Result findById(@PathVariable int id) {
        CourseClassify courseClassify = courseClassifyService.findById(id);
        return Result.success(courseClassify);
    }
    @GetMapping("/courseclassify/page")
    public Result findByPage(

            @RequestParam(value = "pageNum",required = false,
                    defaultValue = "1")
            Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "5")
            Integer pageSize,
            CourseClassify courseClassify
    ){
        Map<String,Object> map = courseClassifyService.findByPage(pageNum,pageSize,courseClassify);
        return Result.success(map);
    }

}



